#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://blog.csdn.net/yangxing2/article/details/78637937
# 用Python作画之彩虹
'''
颜色空间
RGB模型：光的三原色，共同决定色相
HSB/HSV模型：H色彩，S深浅，B饱和度，H决定色相
需要将HSB模型转换为RGB模型
'''

import turtle


def HSB2RGB(hues):
    hues = hues * 3.59  # 100转成359范围
    rgb = [0.0, 0.0, 0.0]
    i = int(hues / 60) % 6
    f = hues / 60 - i
    if i == 0:
        rgb[0] = 1
        rgb[1] = f
        rgb[2] = 0
    elif i == 1:
        rgb[0] = 1 - f
        rgb[1] = 1
        rgb[2] = 0
    elif i == 2:
        rgb[0] = 0
        rgb[1] = 1
        rgb[2] = f
    elif i == 3:
        rgb[0] = 0
        rgb[1] = 1 - f
        rgb[2] = 1
    elif i == 4:
        rgb[0] = f
        rgb[1] = 0
        rgb[2] = 1
    elif i == 5:
        rgb[0] = 1
        rgb[1] = 0
        rgb[2] = 1 - f
    return rgb


def rainbow(printer):
    hues = 0.0
    printer.color(1, 0, 0)
    # 绘制彩虹
    printer.hideturtle()
    printer.speed(100)
    printer.pensize(3)
    printer.penup()
    printer.goto(-400, -300)
    printer.pendown()
    printer.right(110)
    for i in range(100):
        printer.circle(1000)
        printer.right(0.13)
        hues = hues + 1
        rgb = HSB2RGB(hues)
        printer.color(rgb[0], rgb[1], rgb[2])
    printer.penup()


def main():
    window = turtle.Screen()
    printer = turtle.Turtle()
    window.setup(800, 600, 0, 0)
    window.bgcolor((0.8, 0.8, 1.0))
    window.tracer(False)
    rainbow(printer)
    # 输出文字
    window.tracer(False)
    printer.goto(100, -100)
    printer.pendown()
    printer.color("red")
    printer.write("Rainbow", align="center", font=("Script MT Bold", 80, "bold"))
    window.tracer(True)
    window.mainloop()


if __name__ == "__main__":
    main()
